const Web3 = require('web3')
const solc = require('solc')
const readFile = require('./util.js').readFile


let web3;

if (typeof web3 !== 'undefined') {
    web3 = new Web3(web3.currentProvider);
} else {
    web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}

module.exports = {
    web3: web3
}




// ;(async function(){
//     // 合约代码
//     let source = await readFile(`./contracts/Contract.sol`)
//     let compiledCode = solc.compile(source, 1)
//     // ABI
//     let abiDefinition = JSON.parse(compiledCode.contracts[':token'].interface)
//     // Code
//     let deployCode = compiledCode.contracts[':token'].bytecode
//     // 合约对象
//     let tokenContract = new web3.eth.Contract(abiDefinition)

    
//     let Addrs = await web3.eth.getAccounts()
//     //部署者的地址，当前取默认账户的第一个地址
//     let deployeAddr = Addrs[0]

//     // 部署
//     let newContractInstance = await tokenContract.deploy({
//         data: deployCode,
//         arguments: [123]
//     }).send({from: deployeAddr, gas: 4700000})
//     // 合约地址
//     console.log(newContractInstance.options.address)
//     // 获取某区块交易信息
    
//     // let receipt = await web3.eth.getTransactionReceipt('0x2169139630a257baac3038a4b772f99ab4285480d80d91b6f89a9724e10a2b7a')
//     // // console.log(receipt)

//     // 测试 创建用户
//     // let a = await web3.eth.accounts.create()
//     // console.log(a)
//     // let new_account = await web3.eth.personal.newAccount(a.privateKey)
//     // console.log(new_account)

//     // let new_addrs = await web3.eth.getAccounts()
//     // console.log(new_addrs)


//     // 创建钱包地址
//     let wallet = await web3.eth.personal.newAccount('test_password')
//     console.log(wallet)


    
    
// })()


